package com.mit.wx.util;


import com.mit.wx.util.http.SSLClients;
import com.mit.wx.wx.dto.BinaryPart;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.Map;

/**
 * 2018/10/11 Create by 郭文梁
 * HttpUtil
 * Http工具类
 *
 * @author 郭文梁
 * @date 2018/10/11
 */

public class HttpUtil {
    private static final int STATUS_OK = HttpStatus.SC_OK;
    private static CloseableHttpClient client = SSLClients.createHttpsClient();


    public static String get(String urlStr) throws IOException {
        HttpGet get = new HttpGet(urlStr);

        try (CloseableHttpResponse response = client.execute(get)) {
            if (response.getStatusLine().getStatusCode() == STATUS_OK) {
                return EntityUtils.toString(response.getEntity());
            } else {
                return null;
            }
        }
    }

    public static byte[] downloadAsByteArray(String url) throws IOException {
        HttpGet get = new HttpGet(url);
        try (CloseableHttpResponse response = client.execute(get)) {
            if (response.getStatusLine().getStatusCode() == STATUS_OK) {
                return EntityUtils.toByteArray(response.getEntity());
            } else {
                return null;
            }
        }
    }


    public static String upload(String url, String name, byte[] bytes) throws IOException {
        HttpPost post = new HttpPost(url);
        MultipartEntityBuilder builder = MultipartEntityBuilder.create();
        builder.addBinaryBody(name, bytes);
        post.setEntity(builder.build());
        try (CloseableHttpResponse response = client.execute(post)) {
            if (response.getStatusLine().getStatusCode() == STATUS_OK) {
                return EntityUtils.toString(response.getEntity());
            }
            return null;
        }
    }

    public static String postWithStringBody(String url, String body, String respCharset) throws IOException {
        HttpPost post = new HttpPost(url);
        post.setEntity(new StringEntity(body, "UTF-8"));
        try (CloseableHttpResponse response = client.execute(post)) {
            if (response.getStatusLine().getStatusCode() == STATUS_OK) {
                return EntityUtils.toString(response.getEntity(), respCharset);
            }
            return null;
        }
    }

    public static String postWithStringBody(String url, String body) throws IOException {
        return postWithStringBody(url, body, null);
    }

    /**
     * 使用POST方式上传二进制内容
     *
     * @param url   API Url
     * @param bytes 上传内容
     * @return 回复结果
     * @throws IOException IOException
     */
    public static String postWithByteArray(String url, byte[] bytes) throws IOException {
        HttpPost post = new HttpPost(url);
        post.setEntity(new ByteArrayEntity(bytes));
        try (CloseableHttpResponse response = client.execute(post)) {
            if (response.getStatusLine().getStatusCode() == STATUS_OK) {
                return EntityUtils.toString(response.getEntity());
            } else {
                return null;
            }
        }
    }

    public static String postMultipartFile(String url, Map<String, BinaryPart> files, Map<String, String> formData) throws IOException {
        HttpPost post = new HttpPost(url);
        MultipartEntityBuilder builder = MultipartEntityBuilder.create();
        //加入二进制内容
        for (Map.Entry<String, BinaryPart> entry : files.entrySet()) {
            BinaryPart part = entry.getValue();
            ContentType contentType = ContentType.getByMimeType(part.getContentType());
            if (contentType == null) {
                contentType = ContentType.create(part.getContentType());
            }
            builder.addBinaryBody(entry.getKey(), part.getContent(), contentType, part.getFileName());
        }
        //加入普通表单内容
        ContentType contentType = ContentType.TEXT_PLAIN;
        for (Map.Entry<String, String> entry : formData.entrySet()) {
            builder.addTextBody(entry.getKey(), new String(entry.getValue().getBytes("UTF-8"), "ISO-8859-1"), contentType);
        }
        post.setEntity(builder.build());

        try (CloseableHttpResponse resp = client.execute(post)) {
            if (resp.getStatusLine().getStatusCode() == STATUS_OK) {
                return EntityUtils.toString(resp.getEntity());
            }
        }
        return null;
    }

}